﻿Imports System.IO
Imports System.Text.RegularExpressions

Public Class WatchDog

    Public dogName As String
    Public watchGate As String
    Public boneList As New ArrayList
    Public isPatroling As Boolean

    Private WithEvents watcher As New FileSystemWatcher()

    Public Function GetStatus() As Boolean
        Return watcher.EnableRaisingEvents
    End Function

    Sub New(ByVal MonitorName As String)

        Me.dogName = MonitorName

        watchGate = MainModule.Read(MonitorName, "WATCH_GATE")

        If Not "".Equals(watchGate) Then
            Dim items() As String
            items = MainModule.Read(MonitorName, "BONES").Split(",")

            For Each fileName As String In items

                Dim bone As New Bone
                bone.FileName = fileName

                bone.NeedMonitorCreate = IIf("TRUE".Equals(MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_MONITOR_CREATE).ToUpper), True, False)
                bone.NeedMonitorRename = IIf("TRUE".Equals(MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_MONITOR_RENAME).ToUpper), True, False)
                bone.NeedMonitorDelete = IIf("TRUE".Equals(MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_MONITOR_DELETE).ToUpper), True, False)
                bone.NeedMonitorChange = IIf("TRUE".Equals(MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_MONITOR_CHANGE).ToUpper), True, False)

                bone.CreateMessage = MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_CREATE_MESSAGE)
                bone.RenameMessage = MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_RENAME_MESSAGE)
                bone.DeleteMessage = MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_DELETE_MESSAGE)
                bone.ChangeMessage = MainModule.Read(MonitorName & "@\@" & fileName, MainModule.FILE_CHANGE_MESSAGE)

                boneList.Add(bone)
                bone = Nothing
            Next
        End If
        '
        watcher.Path = watchGate
        watcher.IncludeSubdirectories = True
        watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
        watcher.Filter = "*.*"
        'watcher.EnableRaisingEvents = True
    End Sub

    Sub Start_Watch()
        watcher.EnableRaisingEvents = True
        isPatroling = True
    End Sub

    Sub Stop_Watch()
        watcher.EnableRaisingEvents = False
        isPatroling = False
    End Sub

    ' 
    Private Sub watcher_Renamed(sender As Object, e As RenamedEventArgs) Handles watcher.Renamed

    End Sub

    '
    Private Sub watcher_Created(sender As Object, e As FileSystemEventArgs) Handles watcher.Created

    End Sub

    Private Sub watcher_Changed(sender As Object, e As FileSystemEventArgs) Handles watcher.Changed


        Dim fileName As String

        fileName = GetFileName(e.Name)

        If Not MessageController.Contains(fileName) Then
            Dim msg(2) As String
            msg = GetMessage(fileName, WatchType.Change)
            If Not msg(0) = Nothing And Not msg(1) = Nothing And Not msg(2) = Nothing Then
                MessageController.messagelist.Add(msg)
            End If

        End If

    End Sub

    Private Function GetFileName(ByVal strName As String)


        If strName.IndexOf("\") > 0 Then
            strName = strName.Substring(strName.IndexOf("\") + 1)
        Else
            strName = strName
        End If

        If strName.EndsWith(".xlsx") Or strName.EndsWith(".xls") Then
            If strName.IndexOf("~$") >= 0 Then
                strName = strName.Replace("~$", "")
            End If
        End If

        If strName.EndsWith(".docx") Or strName.EndsWith(".doc") Then
            If strName.IndexOf("~$") >= 0 Then
                strName = strName.Replace("~$", "")
            End If
        End If


        If strName.EndsWith(".trf") Or strName.EndsWith(".trf") Then
            If strName.IndexOf("~$") >= 0 Then
                strName = strName.Replace("~$", "")
            End If
        End If

        GetFileName = strName
    End Function


    Private Function GetMessage(ByVal filename As String, ByVal i As WatchType) As String()
        Dim msg(2) As String


        For Each item As Bone In Me.boneList
            If item.FileName.Equals(filename) Then
                msg(0) = filename
                Select Case i
                    Case WatchType.Create
                        msg(1) = IIf("".Equals(item.CreateMessage), "has been create!!!", item.CreateMessage)
                    Case WatchType.Rename
                        msg(1) = IIf("".Equals(item.RenameMessage), "has been renamed!!!", item.RenameMessage)
                    Case WatchType.Delete
                        msg(1) = IIf("".Equals(item.DeleteMessage), "has been deleted!!!", item.DeleteMessage)
                    Case WatchType.Change
                        msg(1) = IIf("".Equals(item.ChangeMessage), "has been changed!!!", item.ChangeMessage)

                End Select
                msg(2) = i
                Exit For
            End If
        Next
        '
        'If "".Equals(msg(0)) Then
        '    For Each item As Bone In Me.boneList
        '        If item.FileName.IndexOf("*") >= 0 Then
        '            Dim reg As Regex
        '            reg = New Regex(item.FileName.Replace(".", "\."))
        '            If reg.IsMatch(filename) Then
        '                msg(0) = filename
        '                Select Case i
        '                    Case WatchType.Create
        '                        msg(1) = IIf("".Equals(item.CreateMessage), "has been create!!!", item.CreateMessage)
        '                    Case WatchType.Rename
        '                        msg(1) = IIf("".Equals(item.RenameMessage), "has been renamed!!!", item.RenameMessage)
        '                    Case WatchType.Delete
        '                        msg(1) = IIf("".Equals(item.DeleteMessage), "has been deleted!!!", item.DeleteMessage)
        '                    Case WatchType.Change
        '                        msg(1) = IIf("".Equals(item.ChangeMessage), "has been changed!!!", item.ChangeMessage)

        '                End Select
        '                msg(2) = i
        '                Exit For

        '            End If
        '        End If


        '    Next
        'End If

        GetMessage = msg
    End Function


End Class
